package com.sqisland.dash;

import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Binder;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.util.Log;

public class SensorsService extends Service {
  private final IBinder binder = new LocalBinder();

  private AirspeedSensorManager airspeedSensorManager = null;
  private TextSensorManager textSensorManager = null;

  @Override
  public void onCreate() {
    super.onCreate();

    SharedPreferences pref = PreferenceManager
        .getDefaultSharedPreferences(this);
    updateAirspeedSensor(pref);
    updateTextSensor(pref);
  }

  @Override
  public IBinder onBind(Intent intent) {
    return binder;
  }

  /**
   * Class used for the client Binder. Because we know this service always runs
   * in the same process as its clients, we don't need to deal with IPC.
   */
  class LocalBinder extends Binder {
    SensorsService getService() {
      // Return this instance of LocalService so clients can call public methods
      return SensorsService.this;
    }
  }

  private SensorManager[] getManagers() {
    return new SensorManager[] {
        airspeedSensorManager,
        textSensorManager
    };
  }

  private void connectAllDevices() {
    Log.i(Constants.TAG, "connectAllDevices");

    for (SensorManager manager : getManagers()) {
      if (manager == null) {
        continue;
      }
      manager.stopSensor();
      manager.startSensor();
    }
  }

  private void disconnectAllDevices() {
    Log.i(Constants.TAG, "disconnectAllDevices");

    for (SensorManager manager : getManagers()) {
      if (manager == null) {
        continue;
      }
      manager.stopSensor();
    }
  }

  public void toggleDeviceConnections() {
    if (isDisconnected()) {
      connectAllDevices();
    } else {
      disconnectAllDevices();
    }
  }

  public boolean isConfigured() {
    for (SensorManager manager : getManagers()) {
      if (manager != null) {
        return true;
      }
    }
    return false;
  }

  public boolean isDisconnected() {
    for (SensorManager manager : getManagers()) {
      if (manager == null) {
        continue;
      }
      if (manager.getSensorState() == SensorManager.STATE_CONNECTING ||
          manager.getSensorState() ==  SensorManager.STATE_CONNECTED) {
        return false;
      }
    }
    return true;
  }

  public BluetoothSensorManager getSensor(String type) {
    if (AirspeedSensorManager.TYPE.equals(type)) {
      return airspeedSensorManager;
    }
    if (TextSensorManager.TYPE.equals(type)) {
      return textSensorManager;
    }
    throw new IllegalArgumentException("Unsupported type: " + type);
  }

  public void updateSensor(String type) {
    SharedPreferences pref = PreferenceManager
        .getDefaultSharedPreferences(this);
    if (AirspeedSensorManager.TYPE.equals(type)) {
      updateAirspeedSensor(pref);
    }
    if (TextSensorManager.TYPE.equals(type)) {
      updateTextSensor(pref);
    }
  }

  private void updateAirspeedSensor(SharedPreferences pref) {
    if (airspeedSensorManager != null) {
      airspeedSensorManager.stopSensor();
    }

    String key = AirspeedSensorManager.TYPE + Constants.KEY_SENSOR_SUFFIX;
    String row = pref.getString(key, null);
    if (row != null) {
      String[] fields = row.split("\t");
      String address = fields[0];
      String name = fields[1];
      airspeedSensorManager = new AirspeedSensorManager(this, address, name);
    } else {
      airspeedSensorManager = null;
    }
  }

  private void updateTextSensor(SharedPreferences pref) {
    if (textSensorManager != null) {
      textSensorManager.stopSensor();
    }

    String key = TextSensorManager.TYPE + Constants.KEY_SENSOR_SUFFIX;
    String row = pref.getString(key, null);
    if (row != null) {
      String[] fields = row.split("\t");
      String address = fields[0];
      String name = fields[1];
      textSensorManager = new TextSensorManager(this, address, name);
    } else {
      textSensorManager = null;
    }
  }
}